<?php
/*
# Options governing display in the comment form.
add_option('ntt_include_div_wrapper', false, __('Whether the Negative Turing Test section of your comment form should be wrapped in a div. Leaving this off helps prevent spammers from adapting to NTT.'), 'no');
add_option('ntt_include_ids', false, __('Whether the elements (including the div wrapper, if that\'s turned on) in the Negative Turing Test section of your comment form should each have an id attribute. Leaving this off helps prevent spammers from adapting to NTT.'), 'no');
add_option('ntt_main_string', '', __('The default value of the text field, to be edited by the user.'), 'no');
add_option('ntt_instructions', '', __('Instructions for would-be commenters. You may leave this blank if your main string provides sufficient instruction.'), 'no');

# Options governing the formulation of the computed main string.
add_option('ntt_range_start', '', __('The start of the range of the main string that must be replaced or deleted.'), 'no');
add_option('ntt_range_length', '', __('The length of the range of the main string that must be replaced or deleted. If the length of the range is 0, then the user must simply insert text.'), 'no');
add_option('ntt_param_string', '', __('The string with which the user must replace part of the main string (that part being defined by the range). If this string is empty and the range is non-empty, then the required action is simply deletion; if this string is empty and the range is also empty, then the action is a no-op.'), 'no');
 */

function ntt_show_options() {
	if(!current_user_can('manage_options'))
		return;
?>

	<div class="wrap">
	<h2>Negative Turing Test</h2>

	<form id="ntt_options_form" action="<?php echo htmlspecialchars(add_query_arg('action', 'setoptions')); ?>" method="post">

<?php
	if($_SERVER['REQUEST_METHOD'] == 'POST') {
		if($_GET['action'] == 'setoptions')
			check_admin_referer('Negative_Turing_Test-options');
		elseif($_GET['action'] == 'resetcount')
			check_admin_referer('Negative_Turing_Test-resetcount');

		if(isset($_POST['main_string']))
			update_option('ntt_main_string',  $_POST['main_string']);
		if(isset($_POST['range_start'])  && ctype_digit($_POST['range_start']))
			update_option('ntt_range_start',  $_POST['range_start']);
		if(isset($_POST['range_length']) && ctype_digit($_POST['range_length']))
			update_option('ntt_range_length', $_POST['range_length']);
		if(isset($_POST['param_string']))
			update_option('ntt_param_string', $_POST['param_string']);
		if(isset($_POST['instructions']))
			update_option('ntt_instructions', $_POST['instructions']);
		if(isset($_POST['include_ids'])) {
			# Test if it's false. If it's not false, then it's true.
			if(in_array(strtolower($_POST['include_ids']), array('0', 'no', 'false')))
				update_option('ntt_include_ids', false);
			else
				update_option('ntt_include_ids',  true);
		} else
			update_option('ntt_casesensitive', false);
		if(isset($_POST['include_div_wrapper'])) {
			# Test if it's false. If it's not false, then it's true.
			if(in_array(strtolower($_POST['include_div_wrapper']), array('0', 'no', 'false')))
				update_option('ntt_include_div_wrapper', false);
			else
				update_option('ntt_include_div_wrapper',  true);
		} else
			update_option('ntt_casesensitive', false);
		if(isset($_POST['tab_index']))
			update_option('ntt_tabindex', $_POST['tab_index']);
		if(isset($_POST['casesensitive'])) {
			# Test if it's false. If it's not false, then it's true.
			if(in_array(strtolower($_POST['casesensitive']), array('', '0', 'no', 'false')))
				update_option('ntt_casesensitive', false);
			else
				update_option('ntt_casesensitive',  true);
		} else
			update_option('ntt_casesensitive', false);
		if(isset($_POST['disposalaction'])) {
			# Change the disposal action, but only if the one provided is one that we recognize. (Don't let the user make up disposal actions that we don't know how to perform.)
			$disposal_action = strtolower($_POST['disposalaction']);
			if(in_array($disposal_action, array('spam', 'delete')))
				update_option('ntt_disposal_action', $disposal_action);
		}
	}
?>

	<?php wp_nonce_field('Negative_Turing_Test-options'); ?>

	<fieldset class="options">
	<h3>The test</h3>

	<div>
	<label for="main_string"><?php _e('Main string'); ?>: </label>
	<input type="text" name="main_string" value="<?php echo htmlspecialchars(get_option('ntt_main_string')); ?>" size="80" />
	</div>

	<div>
	<label for="range_start"><?php _e('Range start'); ?>: </label>
	<input type="text" name="range_start" value="<?php echo htmlspecialchars(get_option('ntt_range_start')); ?>" size="10" />
	<label for="range_length"><?php _e('Range length'); ?>: </label>
	<input type="text" name="range_length" value="<?php echo htmlspecialchars(get_option('ntt_range_length')); ?>" size="10" />
	<p><?php _e('The range defines the substring of the main string that will be replaced or deleted. If the range\'s length is zero, the action becomes an insertion.'); ?></p>
	</div>

	<div>
	<label for="param_string"><?php _e('Parameter string'); ?>: </label>
	<input type="text" name="param_string" value="<?php echo htmlspecialchars(get_option('ntt_param_string')); ?>" size="80" />
	<p><?php _e('The parameter string is the string with which the user must replace the substring (defined by the range) of the main string. If the parameter string is empty, the action becomes a deletion. (If the range is empty &mdash; that is, its length is zero &mdash; then the action is a no-op, and you will not be blocking spammers.)'); ?></p>
	</div>

	<div>
	<label for="instructions"><?php _e('Instructions'); ?>: </label>
	<input type="text" name="instructions" value="<?php echo htmlspecialchars(get_option('ntt_instructions')); ?>" size="80" />
	<p><?php _e('Instructions for would-be commenters. You may leave this blank if your main string provides sufficient instruction.'); ?></p>
	</div>
	</fieldset>

	<div id="ntt_preview">
	<h4>Preview of computed string (not live&mdash;updates on Save)</h4>
	<p>Note: Invisible characters are shown as visible.</p>

	<p>Challenge: <?php echo ntt_show_invisibles(htmlspecialchars(get_option('ntt_main_string'))); ?></p>
	<p>Correct response: <?php echo ntt_show_invisibles(htmlspecialchars(ntt_compute(get_option('ntt_main_string'), get_option('ntt_range_start'), get_option('ntt_range_length'), get_option('ntt_param_string')))); ?></p>
	</div>

	<fieldset class="options">
	<div>
	<input type="checkbox" name="casesensitive"<?php if(get_option('ntt_casesensitive')) echo ' checked="checked"'; ?>> <?php _e('Case-sensitive'); ?></input>
	<p><?php echo htmlspecialchars(__('Whether the response should be compared to the computed main string in a case-sensitive manner. Case-sensitive comparison considers letters in different cases to be inequal; case-insensitive comparison (the default) considers them the same. For example, “A” is in the upper case, whereas “a” is in the lower case; they are the same letter when compared case-insensitively, and they are different letters when compared case-sensitively.')); ?></p>
	</div>

	<fieldset class="options">
	<div>
	<h3>Disposal options</h3>
	<label for="disposalaction"><?php _e('Disposal method'); ?>: </label>
	<input type="radio" name="disposalaction" value="spam"<?php if(get_option('ntt_disposal_action') == 'spam') echo ' checked="checked"'; ?>>Mark it as spam</input>
	<input type="radio" name="disposalaction" value="delete"<?php if(get_option('ntt_disposal_action') == 'delete') echo ' checked="checked"'; ?>>Delete it</input>
	<p><?php echo htmlspecialchars(__("Whether to react to a failed Turing test by marking the comment as spam, or by deleting it entirely. You would use the former action to help train another spam-prevention system such as Akismet; if you're using NTT by itself, then you should simply delete spam comments so that they don't pile up.")); ?></p>
	</div>
	</fieldset>

	<fieldset class="options">
	<h3>Display configuration</h3>

	<div><input type="checkbox" name="include_div_wrapper" value="yes" <?php if(get_option('ntt_include_div_wrapper')) echo 'checked="checked"'; ?> /><label for="include_div_wrapper"><?php _e('Include div wrapper'); ?></label></div>
	<div><input type="checkbox" name="include_ids" value="yes" <?php if(get_option('ntt_include_ids')) echo 'checked="checked"'; ?> /><label for="include_ids"><?php _e('Include &ldquo;id&rdquo; attributes'); ?></label></div>

	<p><?php _e('Turning either of these settings on makes the NTT section of your comment form more stylable, but also easier for spammers to find and load an answer into. While I\'d hope that NTT itself provides greater protection, I also welcome the added protection &mdash; however lightweight &mdash; of hiding the fields from the spammers whenever possible.'); ?></p>

	<div id="ntt_tabindex">
	<label for="tab_index"><?php _e('Tab order of response field'); ?>: </label>
	<input type="text" name="tab_index" size="10" value="<?php echo get_option('ntt_tabindex'); ?>" />
	</div>

	</fieldset>

	<div class="submit">
	<button type="submit">Save changes</button>
	</div>

	</form>

	<div id="ntt_number_of_spams_blocked">
	<h3>Number of spams blocked</h3>
	<?php
		if($_GET['action'] == 'resetcount') {
			update_option('ntt_number_of_spams_blocked', 0);
			?><p>Count reset!</p><?php
		}
	?><p>Negative Turing Test has foiled <?php echo htmlspecialchars(get_option('ntt_number_of_spams_blocked')); ?> attempts to spam your blog.</p>

	<form action="<?php echo htmlspecialchars(add_query_arg('action', 'resetcount')); ?>" method="POST" style="display: block">
	<?php wp_nonce_field('Negative_Turing_Test-resetcount'); ?>

	<div class="submit">
	<button type="submit">Reset count</button>
	</div>

	</form>
	</div><?php /*Number of spams blocked*/ ?>

	</div><?php /*Negative Turing Test section div*/ ?>

	<?php
} //function ntt_show_options
?>
